package com.wyb.hzmt.static_baseFrame_tools0.tools;

import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.wyb.hzmt.static_baseFrame_tools0.framework.MyApplication;

/**
 * Created by wyb on 2017/8/9.
 */

public class MyToast {
    static MyApplication mp = MyApplication.getInstance();
    static Handler handler = mp.getHandler();
    static Toast toast;

    /**
     * 4、Toast方法组，方法数共2
     * 无视主、子线程的toast
     * @param str 显示的内容
     * @param single 是否服用
     * @param duration 时长
     */
    private static final boolean allow = true;//

    //        private static final boolean allow = false;//
    public  static void toastSingleLongShow(final String str, final boolean single , final int duration){
        if (allow){
            if(Thread.currentThread() == Looper.getMainLooper().getThread()){
                showSingleLong(str,single,duration);
            } else{
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        showSingleLong(str,single,duration);
                    }
                });
            }
        }
    }


    /**
     * 自己决定是否复用的toast
     * @param str 显示的内容
     * @param single 是否服用
     * @param duration 时长
     */
    private  static void showSingleLong(String str ,boolean single ,int duration ){
        if(single){
            if ( toast == null){
                toast= Toast.makeText(mp,str, duration);
            }
            toast.setText(str);
        }else {
            toast= Toast.makeText(mp,str, duration);
        }
        toast.show();
    }

}
